﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using JulMar.Windows.Mvvm;
using MediaBrowser.Gui.MvvmUtils;
using MediaBrowser.Gui.MvvmUtils.UI;

namespace MediaBrowser.Gui.ViewModels
{
    /// <summary>
    /// Main View-Model 
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly ObservableCollection<PageViewModelBase> views;
        private PageViewModelBase currentContent;
        private bool isGoForwardView;
        private IWindowAboutVisualizer windowAboutVisualizer;

        public ObservableCollection<PageViewModelBase> Views
        {
            get { return this.views; }
        }

        public PageViewModelBase CurrentContent
        {
            get
            {
                return this.currentContent;
            }
            private set
            {
                this.currentContent = value;
                OnPropertyChanged("CurrentContent");
            }
        }

        public bool IsGoForwardView
        {
            get
            {
                return isGoForwardView;
            }
            set
            {
                isGoForwardView = value;
                OnPropertyChanged("IsGoForwardView");
            }
        }

        #region Command

        public ICommand ForwardViewCommand
        {
            get;
            private set;
        }

        public ICommand BackwardViewCommand
        {
            get;
            private set;
        }

        public ICommand HomeViewCommand
        {
            get;
            private set;
        }

        public ICommand AboutCommand
        {
            get;
            private set;
        }

        #endregion

        public MainViewModel(PageHomeViewModel pageHomeViewModel,
                             PageMoviesViewModel pageMoviesViewModel,
                             PageMusicViewModel pageMusicViewModel,
                             PagePicturesViewModel pagePicturesViewModel,
                             PageBooksViewModel pageSettingsViewModel)
            : base()
        {

            //Init View
            this.views = new ObservableCollection<PageViewModelBase>();
            this.views.Add(pageHomeViewModel);
            this.views.Add(pageMoviesViewModel);
            this.views.Add(pageMusicViewModel);
            this.views.Add(pagePicturesViewModel);
            this.views.Add(pageSettingsViewModel);
            this.CurrentContent = this.views[0];

            //Init Command
            ForwardViewCommand = new DelegatingCommand(OnForwardView, CanForwardView);
            BackwardViewCommand = new DelegatingCommand(OnBackwardView, CanBackwardView);
            HomeViewCommand = new DelegatingCommand(OnHomeView);
            AboutCommand = new DelegatingCommand(OnAbout);

            RegisterWithMessageMediator();

            windowAboutVisualizer = Resolve<IWindowAboutVisualizer>();
        }

        /// <summary>
        /// Forward View
        /// </summary>
        private void OnForwardView()
        {
            IsGoForwardView = true;
            int index = this.views.IndexOf(CurrentContent);
            this.CurrentContent = this.views[index + 1];
        }

        /// <summary>
        /// Contrôle Forward View
        /// </summary>
        /// <returns></returns>
        private bool CanForwardView()
        {
            int index = this.views.IndexOf(CurrentContent);
            return !(index + 1 == views.Count);
        }

        /// <summary>
        /// Backward View
        /// </summary>
        private void OnBackwardView()
        {
            IsGoForwardView = false;
            int index = this.views.IndexOf(CurrentContent);
            this.CurrentContent = this.views[index - 1];
        }

        /// <summary>
        /// Contrôle Forward View
        /// </summary>
        /// <returns></returns>
        private bool CanBackwardView()
        {
            int index = this.views.IndexOf(CurrentContent);
            return !(index == 0);
        }

        /// <summary>
        /// Home View
        /// </summary>
        private void OnHomeView()
        {
            IsGoForwardView = false;
            this.CurrentContent = this.views[0];
        }

        /// <summary>
        /// Fenêtre A propos de
        /// </summary>
        private void OnAbout()
        {
            if (windowAboutVisualizer != null)
            {
                windowAboutVisualizer.Show();
            }
        }

        /// <summary>
        /// Message permettant la modification de la page courant
        /// </summary>
        /// <param name="page"></param>
        [MessageMediatorTarget(MediatorMessages.UpdateCurrentView)]
        private void OnLoadPage(PageViewModelBase page)
        {
            IsGoForwardView = true;
            this.CurrentContent = this.views[views.IndexOf(page)];
        }
    }
}
